package com.ronan.client.handler;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.ronan.common.constant.BusinessConstant;
import com.ronan.common.constant.Constants;
import com.ronan.common.entity.business.GameValue;
import com.ronan.common.proto.Business;
import com.ronan.common.proto.Heart;
import com.ronan.common.proto.Login;
import com.ronan.common.proto.Message;
import com.ronan.common.protocol.RIMHeader;
import com.ronan.common.protocol.RIMProtocol;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * FileName    : com.ronan.client.handler
 * Description :
 *
 * @author : ronan
 * @version : 1.0
 * Create Date : 2021/3/1 16:55
 **/
@Slf4j
@ChannelHandler.Sharable
public class RIMClientHandler extends SimpleChannelInboundHandler<RIMProtocol> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // RIMProtocol login = login();
        // log.info("登录信息 ==> {}",login);
        // ctx.writeAndFlush(login);

        // 发送游戏数据
        ctx.writeAndFlush(business());
    }


    /**
     * 业务数据
     *
     * @return {@link RIMProtocol}
     */
    public RIMProtocol business() {
        RIMProtocol biz = new RIMProtocol();
        GameValue val = new GameValue();
        val.setMood(100);
        val.setWhite(100);
        val.setWear(100);
        Business gameData = Business.newBuilder()
                .setFrom(IdUtil.simpleUUID())
                .setType(BusinessConstant.GAMEVALUE)
                .setBody(JSONUtil.toJsonStr(val))
                .build();
        biz.setBody(gameData);
        RIMHeader header = new RIMHeader();
        header.setMsgNo(System.currentTimeMillis());
        header.setCommand(Constants.MsgConstant.BUSINESS);
        header.setTimestamp(System.currentTimeMillis());
        header.setLength(gameData.toByteArray().length);
        biz.setHeader(header);
        log.info("业务数据 ==> {}", biz);
        return biz;
    }


    /**
     * 登录数据包
     *
     * @return {@link RIMProtocol}
     */
    public RIMProtocol login() {
        // 登录
        RIMProtocol protocol = new RIMProtocol();
        Login login = Login.newBuilder().setAccid("57aa4c2ad1914c23af4bc58076617429").setToken("c85480a0-d00f-43e4-9bed-63d7bc002b71").build();
        protocol.setBody(login);
        RIMHeader header = new RIMHeader();
        header.setLength(login.toByteArray().length);
        header.setCommand(Constants.MsgConstant.LOGIN);
        protocol.setHeader(header);
        log.info("[登录]:{}", protocol);
        return protocol;
    }

    /**
     * 心跳包
     *
     * @return {@link RIMProtocol}
     */
    public RIMProtocol heartbeat() {
        RIMProtocol heartPro = new RIMProtocol();
        Heart heart = Heart.newBuilder().setHeartTime(System.currentTimeMillis()).build();
        heartPro.setBody(heart);
        RIMHeader heartbeat = new RIMHeader();
        heartbeat.setCommand(Constants.MsgConstant.HEART);
        heartbeat.setLength(heart.toByteArray().length);
        heartPro.setHeader(heartbeat);
        return heartPro;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RIMProtocol protocol) throws Exception {

        if (Constants.MsgConstant.HEART == protocol.getHeader().getCommand()) {
            Heart heart = (Heart) protocol.getBody();
            System.out.println("收到服务器心跳数据: " + heart.getHeartTime() + " ms");
        }

        if (Constants.MsgConstant.MESSAGE == protocol.getHeader().getCommand()) {
            Message msg = (Message) protocol.getBody();
            log.info("消息内容:{}", msg.getBody());
        }

    }
}
